﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace ISO
{
    class Camera
    {
        SpriteBatch _spriteBatch;
        Map _map;
        Player _player;
        int[] _mouse_index;

        #region Constructors
        public Camera(SpriteBatch spritebatch, Player _new_player, Map _new_map)
        {
            this._spriteBatch = spritebatch;
            this._player = _new_player;
            this._map = _new_map;
            this._mouse_index = new int[2];
            this._mouse_index[0] = (int)_player.X_Index - (int)_map.CurrentTiles[0];
            this._mouse_index[1] = (int)_player.Y_Index - (int)_map.CurrentTiles[1];

        }
        #endregion

        #region Methods
        public void DrawCharacter(Character _character)
        {
            try
            {
                if (Visible(_character))
                {
                    int _adjusted_x = (int)(_character.X_Index - _map.CurrentTiles[0]);
                    int _adjusted_y = (int)(_character.Y_Index - _map.CurrentTiles[1]);
                    _spriteBatch.Draw(_character.Texture, new Rectangle(_map.TileCoordinates[_adjusted_x,
                            _adjusted_y].X, _map.TileCoordinates[_adjusted_x, _adjusted_y].Y,
                            _character.Texture.Width / 2, (_character.Texture.Height-84) / 2), new Rectangle(0,84,_character.Texture.Width, _character.Texture.Height-84), Color.White);
                }
            }
            catch (NullReferenceException e)
            {
            }
            /*
            if (_map.CurrentTiles[0] > _map.Num_Visible_Per_Row && _map.CurrentTiles[1] > _map.Num_Visible_Per_Row &&
                _map.CurrentTiles[0] < _map.Num_Total_Per_Row-_map.Num_Visible_Per_Row &&
                _map.CurrentTiles[1] < _map.Num_Total_Per_Row - _map.Num_Visible_Per_Row)
            {
                _spriteBatch.Draw(_character_texture, new Rectangle(_map.TileCoordinates[4,4].X, 
                    _map.TileCoordinates[4,4].Y - 3, _character_texture.Width / 2, _character_texture.Height / 2),
                    Color.White);
            }
            else
            {
                _spriteBatch.Draw(_character_texture, new Rectangle(_map.TileCoordinates[(int)_player.X_Index,
                    (int)_player.Y_Index].X, _map.TileCoordinates[(int)_player.X_Index, (int)_player.Y_Index].Y - 3,
                    _character_texture.Width / 2, _character_texture.Height / 2), Color.White);
            }*/
        }

        public void DrawMouse(Texture2D _text, int _x, int _y)
        {
            _mouse_index = GetMouseIndex(_x, _y);
            //Handles map scrolling with the mouse
            if (_mouse_index[0] == 1 && _map.CurrentTiles[0] > 0)
                _map.CurrentTiles[0] -= 0.125;
            if (_mouse_index[0] == (_map.Num_Visible_Per_Row - 2) && _map.CurrentTiles[0] < (_map.Num_Total_Per_Row - _map.Num_Visible_Per_Row))
                _map.CurrentTiles[0] += 0.125;
            if (_mouse_index[1] == 1 && _map.CurrentTiles[1] > 0)
                _map.CurrentTiles[1] -= 0.125;
            if (_mouse_index[1] == (_map.Num_Visible_Per_Row - 2) && _map.CurrentTiles[1] < (_map.Num_Total_Per_Row - _map.Num_Visible_Per_Row))
                _map.CurrentTiles[1] += 0.125;
            if (_mouse_index[0] == 0 && _map.CurrentTiles[0]>0)
                _map.CurrentTiles[0]-= 0.25;
            if (_mouse_index[0] == (_map.Num_Visible_Per_Row-1) && _map.CurrentTiles[0] < (_map.Num_Total_Per_Row-_map.Num_Visible_Per_Row))
                _map.CurrentTiles[0]+= 0.25;
            if (_mouse_index[1] == 0 && _map.CurrentTiles[1] > 0)
                _map.CurrentTiles[1]-= 0.25;
            if (_mouse_index[1] == (_map.Num_Visible_Per_Row - 1) && _map.CurrentTiles[1] < (_map.Num_Total_Per_Row - _map.Num_Visible_Per_Row))
                _map.CurrentTiles[1]+= 0.25;
            //Draws the cursor area to the screen
            _spriteBatch.Draw(_text, new Rectangle(_map.TileCoordinates[_mouse_index[0], _mouse_index[1]].X, _map.TileCoordinates[_mouse_index[0], _mouse_index[1]].Y, (_text.Width / 2), (_text.Height - 84) / 2), new Rectangle(0, 84, _text.Width, _text.Height - 84), Color.White);
        }

        public int[] GetMouseIndex(int _x, int _y)
        {
            int[] _index = new int[2];
            for (int i = 0; i < _map.Num_Visible_Per_Row; i++)
            {
                for (int j = 0; j < _map.Num_Visible_Per_Row; j++)
                {
                    if (_x >= _map.TileCoordinates[i, j].X && _x < _map.TileCoordinates[i + 1, j].X &&
                        _y >= _map.TileCoordinates[i, j].Y && _y < _map.TileCoordinates[i, j + 1].Y)
                    {
                        _index[0] = i; _index[1] = j;
                        return _index;
                    }
                }
            }
            return _mouse_index;
        }

        public bool Visible(Character _char)
        {
            if (_char.X_Index - _map.CurrentTiles[0] < _map.Num_Visible_Per_Row &&
                    _char.X_Index - _map.CurrentTiles[0] >= 0 &&
                    _char.Y_Index - _map.CurrentTiles[1] < _map.Num_Visible_Per_Row &&
                    _char.Y_Index - _map.CurrentTiles[1] >= 0)
                return true;
            else return false;
        }

        public void DrawMap(Texture2D _tile_texture)
        {
            int _texture_x = 0;
            int _texture_y = 0;
            int _current_x = 0;
            int _current_y = 0;
            Tile _tile_to_draw = new Tile(0,0);

            for (int _x = 0; _x < _map.Num_Visible_Per_Row; _x++)
            {
                for (int _y = 0; _y < _map.Num_Visible_Per_Row; _y++)
                {
                    _tile_to_draw = _map.Tiles[((int)_map.CurrentTiles[0]+ _x), ((int)_map.CurrentTiles[1] + _y)];
                    _texture_x = (_tile_to_draw.Width + _tile_to_draw.XInterval) * _tile_to_draw.XIndex + _tile_to_draw.XInterval;
                    _texture_y = (_tile_to_draw.Height + _tile_to_draw.YInterval) * _tile_to_draw.YIndex + _tile_to_draw.YInterval;
                    _current_x = _map.TileCoordinates[_x,_y].X;
                    _current_y = _map.TileCoordinates[_x,_y].Y;
                    _spriteBatch.Draw(_tile_texture, new Rectangle(_current_x, _current_y, _tile_to_draw.Width / 2,
                        _tile_to_draw.Height / 2), new Rectangle(_texture_x, _texture_y, _tile_to_draw.Width,
                        _tile_to_draw.Height), Color.White);
                    /*
                    _tile_vertices[_x * 2, _y * 2].X = _current_x;
                    _tile_vertices[_x * 2, _y * 2].Y = _current_y;
                    _tile_vertices[_x * 2 + 1, _y * 2].X = _current_x + _tile_to_draw.Width / 8;
                    _tile_vertices[_x * 2 + 1, _y * 2].Y = _current_y + _tile_to_draw.Height / 16 + 2;
                    _tile_vertices[_x * 2, _y * 2 + 1].X = _current_x - _tile_to_draw.Width / 8;
                    _tile_vertices[_x * 2, _y * 2 + 1].Y = _current_y + _tile_to_draw.Height / 8;
                    _tile_vertices[_x * 2 + 1, _y * 2 + 1].X = _tile_vertices[_x * 2, _y * 2 + 1].X + _tile_to_draw.Width / 8;
                    _tile_vertices[_x * 2 + 1, _y * 2 + 1].Y = _tile_vertices[_x * 2 + 1, _y * 2].Y + _tile_to_draw.Height / 16 + 2;*/
                    
                }
            }
        }
        #endregion
    }
}
